package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailBaseInfoService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.explain.FormDataDetailFormExplainHandleName;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.model.ContractModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/12 13:10
 */
@Service("contractDetailBaseInfoServiceImpl")
public class ContractDetailBaseInfoServiceImpl extends AbstractCrmDetailBaseInfoService {


    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailFormExplainHandleName formDataDetailFormExplainHandleName;
    @Resource
    private ContractModel contractModel;
    @Resource
    private PaasAppModel paasAppModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }





    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {

    }

    @Override
    public void handleDetailSpecialExplain( PaasFormDataEntityExt formDataEntity, List<FieldAttrEntity> explainList) throws XbbException {
        Boolean isJxcUse = paasAppModel.validateAppEnable(null, XbbRefTypeEnum.JXC.getAlias(), SaasMarkEnum.SAAS.getCode(), formDataEntity.getCorpid());
        if (!isJxcUse) {
            explainList.removeIf(fieldAttrEntity ->
                    Objects.equals(fieldAttrEntity.getAttr(), ContractEnum.SHIP_STATUS.getAttr()));
        }
    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt formData, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        list.forEach(item->explainMap.put(item.getAttr(), item));
        JSONObject data = commonAnalysisAllData(formData, handlerExplainDTO, handlerExplainVO, list, explainMap);
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        saasAnalysisDataDTO.setFormId(formData.getFormId());
        contractModel.formatData(saasAnalysisDataDTO, data);
        return data;
    }



}
